html_text = """
<!DOCTYPE html>
<html>
<head lang='en'>
 <meta charest='utf-8'>
 <title></title>
</head>
<body>
 <div id="test-1">需要的内容1</div>
 <div id="test-2">需要的内容2</div>
 <div id="testfault">需要的内容3</div>
</body>
</html>
"""

from lxml import etree

# 安装lxml  pip install lxml -i https://pypi.mirrors.ustc.edu.cn/simple/

# 将html文本转换成html对象
html = etree.HTML(html_text)

# 使用xpath解析数据
# text()：取出文本内容

# 从根节点查找
print(html.xpath("/html/body/div/text()"))

# 在整个文档中查找
print(html.xpath("//div/text()"))

# 按属性的条件查询
print(html.xpath("//div[@id='test-1']/text()")[0])

# starts-with ： 查询属性以什么开始的标签
print(html.xpath("//div[starts-with(@id,'test-')]/text()"))
# ends-with ： 查询属性以什么结束的标签
print(html.xpath("//div[ends-with(@id,'1')]/text()"))

xml_text = """
<bookstore>
 <book>
 <title lang="eng">Harry Potter</title>
 <price>29.99</price>
 </book>
 <book>
 <title lang1="eng">Learning XML</title>
 <price>39.95</price>
 </book>
</bookstore>
"""

xml = etree.XML(xml_text)

# book[1] 获取第一个元素
print(xml.xpath("/bookstore/book[1]/title/text()"))
# book[last()] : 最后一个
print(xml.xpath("/bookstore/book[last()]/title/text()"))
# book[last()-1]: 获取倒数第二个
print(xml.xpath("/bookstore/book[last()-1]/title/text()"))
# book[position()<3]： 获取前两个
print(xml.xpath("/bookstore/book[position()<3]/title/text()"))
# 获取带long属性的title标签
print(xml.xpath("//title[@lang]/text()"))
# 获取带long属性等于eng的title标签
print(xml.xpath("//title[@lang='eng']/text()"))
# 获取lang属性中是否包含n字母
print(xml.xpath("//title[contains(@lang,'n')]/text()"))
